package com.zh.freechat.assemble;


import com.zh.freechat.common.resp.FileVO;
import com.zh.freechat.domain.file.entity.MsgFile;
import com.zh.mycommon.util.MyFileUtil;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author ZH
 * @Date 18:47 2020/7/18
 */
public final class PictureWrapper {

    @NotNull
    public static MsgFile createMsgFile(MultipartFile file, String pictureUrl) throws IOException {
        MsgFile msgFile = new MsgFile();
        msgFile.setId(MyFileUtil.fileMD5(file.getInputStream()));
        msgFile.setSize(file.getSize());
        msgFile.setFilename(file.getOriginalFilename());
        msgFile.setMimetype(file.getContentType());
        msgFile.setUrl(pictureUrl);
        return msgFile;
    }

    @NotNull
    public static FileVO convertVO(MsgFile entity) {
        var vo = new FileVO();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

}
